package com.wumai.keystrokes.gui;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.wumai.keystrokes.gui.events.IRefreshKeyGroupListPanelCallback;
import com.wumai.keystrokes.models.settings.KeyGroup;
import com.wumai.keystrokes.utils.KeyConfigManger;
import com.wumai.modernguilib.component.Button;
import com.wumai.modernguilib.component.TextLabel;
import com.wumai.modernguilib.panel.ScrollablePanel;
import com.wumai.modernguilib.panel.WeightBasedView;

public class KeyGroupItem extends ScrollablePanel.ScrollableListItem {

    private WeightBasedView keyGroupItemView;
    private KeyGroup keyGroup;
    private Button editButton;
    private Button deleteButton;
    private int index;
    private IRefreshKeyGroupListPanelCallback callback;

    public KeyGroupItem(KeyGroup keyGroup, int index, IRefreshKeyGroupListPanelCallback callback) {
        super(); // 使用默认构造函数
        this.keyGroup = keyGroup;
        keyGroupItemView = new WeightBasedView(WeightBasedView.Orientation.HORIZONTAL);
        keyGroupItemView.setPadding(0.0f);
        keyGroupItemView.setSpacing(0.0f);
        keyGroupItemView.addChild(new TextLabel(keyGroup.getName()), 0.65f);
        this.callback = callback;
        
        editButton = new Button("Edit") {
            @Override
            public void onClick() {
                // TODO: 实现编辑按键组的逻辑
                System.out.println("Edit key group: " + index);
            }
        };
        editButton.setTextScale(0.5f);
        keyGroupItemView.addChild(editButton, 0.15f);
        
        deleteButton = new Button("Delete") {
            @Override
            public void onClick() {
                // TODO: 实现删除按键组的逻辑
                KeyConfigManger.getInstance().deleteKeyGroup(index);
                // 删除之后通过传递的引用调用父组件刷新列表
                callback.refreshKeyGroupList();
                System.out.println("Delete key group: " + index);
            }
        };
        deleteButton.setTextScale(0.5f);
        keyGroupItemView.addChild(deleteButton, 0.2f);
        this.addChild(keyGroupItemView);
    }

    @Override
    protected void renderContent(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        keyGroupItemView.render(matrixStack, mouseX, mouseY, partialTicks);
    }

    @Override
    protected void renderBackground(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {

    }
}